package com.cn.wanxi.servlet;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.model.FileUploadModel;
import com.cn.wanxi.model.ResultModel;
import com.cn.wanxi.util.FileUpload;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

@WebServlet("/upload")
public class UploadServlet extends HttpServlet {

    private static String direcoty = "/upload/";

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String imgHref = FileUpload.upload(req, direcoty);
        FileUploadModel fileUploadModel = new FileUploadModel();
        fileUploadModel.setSrc(imgHref);
        ResultModel resultModel = ResultModel.getResultModel("", 0, null, fileUploadModel);
        resp.getWriter().print(JSONObject.toJSONString(resultModel));
    }
}
